//
//  YDBaseOptionsStockModeController.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2018/3/14.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDBaseOptionsStockModeController.h"
#import "YDBaseStockHeaderView.h"
#import "YDOptionsModeController.h"
#import "YDStockModeController.h"


@interface YDBaseOptionsStockModeController ()<UIScrollViewDelegate , SYBaseStockHeaderViewDelegate>

@property (nonatomic , assign) CGFloat safeTopH;//顶部导航栏的高度
@property (nonatomic , assign) CGFloat headerViewH;//顶部工具栏高度
@property (nonatomic , weak) YDBaseStockHeaderView *headerView;

@property (nonatomic , weak) UIScrollView *scrollerView;




@end

@implementation YDBaseOptionsStockModeController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.safeTopH = isPhoneX ? 88 : 64;
    self.headerViewH = isPhoneX ? rateX812(40) : rate750(40);
    self.title = @"点买案例模式";
    
    [self headerView];
    
    [self addChildViewsController];
    
    
    [self scrollerView];
    
    [self addChildViewToScrollerView:0];
    
}


-(YDBaseStockHeaderView *)headerView
{
    if (_headerView == nil) {
        YDBaseStockHeaderView *headerView = [[YDBaseStockHeaderView alloc] init];
        [self.view addSubview:headerView];
        [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view).offset(self.safeTopH);
            make.height.equalTo(@(self.headerViewH));
        }];
        headerView.delegate = self;
        _headerView = headerView;
    }
    return _headerView;
}

-(void)addChildViewsController
{
    YDStockModeController *oneVc = [[YDStockModeController alloc] init];
    
    [self addChildViewController:oneVc];
    
    YDOptionsModeController *twoVc = [[YDOptionsModeController alloc] init];
    [self addChildViewController:twoVc];
    
}

-(UIScrollView *)scrollerView
{
    if (_scrollerView == nil) {
        UIScrollView *scrollerView = [[UIScrollView alloc] init];
        [self.view addSubview:scrollerView];
        
        scrollerView.frame = CGRectMake(0, self.safeTopH + self.headerViewH, LKScreenW , LKScreenH - self.safeTopH - self.headerViewH);
        scrollerView.backgroundColor = [UIColor colorWithHexString:mainGray];
        scrollerView.scrollsToTop = NO;
        scrollerView.scrollEnabled = YES;
        scrollerView.bounces = NO;
        scrollerView.delegate = self;
        scrollerView.showsHorizontalScrollIndicator = NO;
        scrollerView.showsVerticalScrollIndicator = NO;
        scrollerView.pagingEnabled = YES;
        if (isIos11) {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        scrollerView.contentSize = CGSizeMake(self.childViewControllers.count * scrollerView.width, 0);
        _scrollerView = scrollerView;
    }
    return _scrollerView;
}

-(void)addChildViewToScrollerView:(NSInteger)index
{
    UIViewController *childView = self.childViewControllers[index];
    if (childView.view.superview) return;
    childView.view.frame = CGRectMake(index * self.scrollerView.width, 0, self.scrollerView.width, self.scrollerView.height);
    [self.scrollerView addSubview:childView.view];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = self.scrollerView.contentOffset.x / self.scrollerView.width;
    
    
    [self addChildViewToScrollerView:index];
    
    [self.headerView clickButtonWithIndex:index];
    
}


#pragma SYBaseStockHeaderViewDelegate
-(void)baseStockHeaderView:(YDBaseStockHeaderView *)headerView didClickButtonTag:(NSInteger)buttonTag
{

//
    
    [UIView animateWithDuration:0.25 animations:^{
        //点击按钮,子控制器切换
        CGPoint offset = self.scrollerView.contentOffset;
        offset.x = buttonTag * self.scrollerView.width;
        self.scrollerView.contentOffset = offset;
        
    } completion:^(BOOL finished) {
        
        [self addChildViewToScrollerView:buttonTag];
        
    }];
}

@end
